package com.horsen.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.horsen.common.util.R;
import com.horsen.service.edu.controller.BaseController;
import com.horsen.service.edu.model.BO.TeacherOutputBO;
import com.horsen.service.edu.model.DTO.TeacherInsertDTO;
import com.horsen.service.edu.model.DTO.TeacherQueryDTO;
import com.horsen.service.edu.model.DTO.TeacherUpdateDTO;
import com.horsen.service.edu.model.Teacher;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 讲师 前端控制器
 *
 * @author Horsen
 */
@Api(tags = "讲师管理")
@ApiSupport(order = 2)
@RestController("AdminTeacher")
@RequestMapping("/admin/edu/teachers")
public class TeacherController extends BaseController {


    @ApiOperationSupport(order = 1)
    @ApiOperation("获取所有讲师")
    @Cacheable(key = "'adminQueryAllTeachers'", value = "teacherList")
    @GetMapping("/")
    public R queryAll() {
        List<Teacher> list = teacherService.list();
        return list != null && list.size() != 0 ?
                R.ok("获取所有讲师成功").setData("items", list) : R.error("获取所有讲师失败");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("分页获取讲师")
    @GetMapping("/{page}/{limit}")
    public R queryByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("讲师查询对象") TeacherQueryDTO teacherQueryDTO) {
        IPage<Teacher> pageParam = teacherService.pageSelect(page, limit, teacherQueryDTO);
        List<Teacher> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取讲师成功").setData("items", list).setData("total", total) : R.error("分页获取讲师失败");
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("根据ID删除讲师")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam(value = "讲师ID", required = true)
                        @PathVariable String id) {
        // 删除Redis缓存
        redisTemplate.delete("adminQueryAllTeachers");
        redisTemplate.delete("webQueryAllTeachers");
        return teacherService.deleteById(id) ?
                R.ok("删除ID=" + id + "的讲师成功") : R.error("删除ID=" + id + "的讲师失败");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("新增一个讲师")
    @PostMapping("/")
    public R insert(@ApiParam(value = "讲师对象", required = true)
                    @RequestBody TeacherInsertDTO teacherInsertDTO) {
        // 删除Redis缓存
        redisTemplate.delete("adminQueryAllTeachers");
        redisTemplate.delete("webQueryAllTeachers");
        return teacherService.insert(teacherInsertDTO) ?
                R.ok("新增讲师成功") : R.error("新增讲师失败");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("修改一个讲师")
    @PutMapping("/")
    public R update(@ApiParam("讲师对象")
                    @RequestBody TeacherUpdateDTO teacherUpdateDTO) {
        // 删除Redis缓存
        redisTemplate.delete("adminQueryAllTeachers");
        redisTemplate.delete("webQueryAllTeachers");
        return teacherService.updateById(teacherUpdateDTO) ?
                R.ok("修改讲师成功") : R.error("修改讲师失败");
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("根据ID获取一个讲师")
    @GetMapping("/{id}")
    public R queryById(@ApiParam(value = "讲师对象", required = true)
                       @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return teacher != null ?
                R.ok("获取ID=" + id + "的讲师成功").setData("item", teacher) : R.error("获取ID=" + id + "的讲师失败");
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("根据ID批量删除讲师")
    @PostMapping("/batch-delete")
    public R deleteByIds(@ApiParam(value = "讲师ID列表", required = true)
                         @RequestBody List<String> idList) {
        // 删除Redis缓存
        redisTemplate.delete("adminQueryAllTeachers");
        redisTemplate.delete("webQueryAllTeachers");
        return teacherService.removeByIds(idList) ?
                R.ok("批量删除讲师成功") : R.error("批量删除讲师成功");
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("根据姓氏获取讲师姓名列表")
    @GetMapping("/names/{key}")
    public R queryNamesByKey(@ApiParam(value = "讲师姓氏", required = true)
                             @PathVariable("key") String key) {
        List<Map<String, Object>> nameList = teacherService.queryNamesByKey(key);
        return R.ok("获取姓名列表成功").setData("nameList", nameList);

    }

    @ApiOperationSupport(order = 9)
    @ApiOperation("导出讲师到Excel文件")
    @PostMapping("/output")
    public R output(@ApiParam(value = "讲师列表", required = true)
                    @RequestBody List<TeacherOutputBO> list) {
        String filePath = teacherService.output(list);
        return R.ok("导出讲师成功").setData("filePath", filePath);
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation("获取讲师数")
    @GetMapping("/count")
    public R count() {
        Integer count = teacherService.count();
        return R.ok("获取讲师数成功").setData("count", count);
    }
}

